package com.doctor.ui.setting;

import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.doctor.adapter.KnowledgeUpdateAdapter;
import com.doctor.bean.KnowledgeBean;
import com.doctor.comm.ConstConfig;
import com.doctor.comm.DialogHelper;
import com.doctor.comm.LoadingTip;
import com.doctor.database.DbOperator;
import com.doctor.net.NetUtil;
import com.doctor.ui.BaseActivity;
import com.doctor.ui.R;
import com.doctor.ui.medicalknowledge.KnowledgeFirstFrag;

public class KnowledgeUpdateActivity extends BaseActivity{

	/**
	 * 医学知识库升级
	 */
	
	private KnowledgeUpdateAdapter adapter;
	private ListView listView;
	private LinearLayout updateBtn,cancelBtn;
	private int dirId=-1;
	private Handler handler;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.knowledge_update_activity);
		
		initiaView();
		getData();
	}
	
	@Override
	protected void initiaView() {
		// TODO Auto-generated method stub
		handler=new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				if (msg.what==3) {
					if (msg.arg1==ConstConfig.SUCCESS) {
						DialogHelper.showConfirmDialog(KnowledgeUpdateActivity.this, "本次医学知识库共升级"+msg.arg2+"篇医学文章！");
					} else if (msg.arg1==ConstConfig.FAILED) {
						DialogHelper.showConfirmDialog(KnowledgeUpdateActivity.this, 
								getResources().getString(R.string.server_error)+"本次医学知识库共升级"+msg.arg2+"篇医学文章！");
					}
				}
			}
		};
		
		listView=(ListView) findViewById(R.id.listView);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				dirId=(Integer) arg1.getTag(R.id.id_tag);
				adapter.setSelectedIndex(arg2);
			}
		});
		
		Click click=new Click();
		updateBtn=(LinearLayout) findViewById(R.id.updateBtn);
		updateBtn.setOnClickListener(click);
		
		cancelBtn=(LinearLayout) findViewById(R.id.cancelBtn);
		cancelBtn.setOnClickListener(click);
	}
	
	@Override
	protected void getData() {
		// TODO Auto-generated method stub			
		List<KnowledgeBean> beanList=DbOperator.getInstance(KnowledgeUpdateActivity.this).selectKnowledgeData(KnowledgeFirstFrag.parentId);
		if (beanList.size()>0) {
			adapter=new KnowledgeUpdateAdapter(KnowledgeUpdateActivity.this, beanList);
			listView.setAdapter(adapter);
		}
	}
	
	private class Click implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v==updateBtn) {
				if (!NetUtil.hasNetwork(KnowledgeUpdateActivity.this)) {
					DialogHelper.showConfirmDialog(KnowledgeUpdateActivity.this, "请连接网络！");
					return;
				}
				if (dirId==-1) {
					DialogHelper.showConfirmDialog(KnowledgeUpdateActivity.this, "请选择要升级的栏目！");
					return;
				}
				LoadingTip.showDialog(KnowledgeUpdateActivity.this, "医学知识库升级......");
				DbOperator.getInstance(KnowledgeUpdateActivity.this).updateArticles(dirId, handler);
			} else if (v==cancelBtn) {
				KnowledgeUpdateActivity.this.finish();
			}
		}		
	}
	
	
}
